<template>
   <el-form label-width="80px">
      <el-form-item label="SKU名称">
         <el-input placeholder="SKU名称" v-model="skuParams.skuName"></el-input>
      </el-form-item>
      <el-form-item label="价格(元)">
         <el-input placeholder="价格(元)" type="number" v-model="skuParams.price"></el-input>
      </el-form-item>
      <el-form-item label="重量(g)">
         <el-input placeholder="重量(g)" type="number" v-model="skuParams.weight"></el-input>
      </el-form-item>
      <el-form-item label="SKU描述">
         <el-input placeholder="SKU描述" type="textarea" v-model="skuParams.skuDesc"></el-input>
      </el-form-item>
      <el-form-item label="平台属性">
         <el-form :inline="true" label-width="80px">
            <el-form-item v-for="(item, index) in attrArr" :label="item.attrName" :key="item.id">
               <el-select v-model="item.attrIdAndValueId">
                  <el-option v-for="item1 in item.attrValueList" :key="item1.id" :label="item1.valueName"
                     :value="`${item.id}:${item1.id}`"></el-option>
               </el-select>
            </el-form-item>
         </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
         <el-form :inline="true" label-width="80px">
            <el-form-item v-for="item in saleArr" :key="item.id" :label="item.saleAttrName">
               <el-select v-model="item.saleIdAndValueId">
                  <el-option v-for="item1 in item.spuSaleAttrValueList" :key="item1.id" :label="item1.saleAttrValueName"
                     :value="`${item.id}:${item1.id}`"></el-option>
               </el-select>
            </el-form-item>
         </el-form>
      </el-form-item>
      <el-form-item label="图片名称">
         <el-table border :data="imgArr" ref="table">
            <el-table-column type="selection" width="80px" align="center"></el-table-column>
            <el-table-column label="图片">
               <template #="{ row, $index }">
                  <img :src="row.imgUrl" alt="" style="width: 100px; height: 100px;">
               </template>
            </el-table-column>
            <el-table-column label="名称" prop="imgName"></el-table-column>
            <el-table-column label="操作">
               <template #="{ row, $index }">
                  <el-button type="primary" @click="handler(row)">设置默认</el-button>
               </template>
            </el-table-column>
         </el-table>
      </el-form-item>
      <el-form-item>
         <el-button type="primary" @click="save">保存</el-button>
         <el-button @click="cancel">取消</el-button>
      </el-form-item>
   </el-form>
</template>

<script setup lang="ts">
// 引入请求的API
import { reqAttr } from '@/api/product/attr'
import { reqSpuImageList, reqSpuHasSaleAttr, reqAddsku } from '@/api/product/spu'
import type { AttrResponseData } from '@/api/product/attr/type'
import type { SpuSaleAttrResponseData, SpuHasImgResponseData, SkuData } from '@/api/product/spu/type'
import { reactive, ref } from 'vue';
import { ElMessage } from 'element-plus';
let $emit = defineEmits(['changeScene'])
// 平台属性
let attrArr = ref<any>([])
// 销售属性
let saleArr = ref<any>([])
// 照片墙的数据
let imgArr = ref<any>([])
// 获取table组件实例
let table = ref<any>()
// 收集sku的参数
let skuParams = reactive<SkuData>({
   // 父组件传递过来的数据
   category3Id: '', //三级分类的id
   spuId: '', //已有的SPU的id
   tmId: '', //SPU品牌的id
   // v-model进行数据收集
   skuName: '',  //sku的名字
   price: '', //sku价格
   weight: '',   //sku重量
   skuDesc: '',  //sku的描述

   skuAttrValueList: [  //平台属性的收集
      // {
      //    attrId: '',   //平台属性的id
      //    valueId: '',  //属性值的id
      // }
   ],
   skuSaleAttrValueList: [ //销售属性的收集
      // {
      //    saleAttrId: '',  //属性id
      //    saleAttrValueId: '',   //属性值id
      // }
   ],
   skuDefaultImg: ''   //sku图片地址
})
// 取消按钮的回调
const cancel = () => {
   $emit('changeScene', { flag: 0, params: 'update' })
}
// 设置默认图片按钮的回调
const handler = (row: any) => {
   // 取消之前的勾选状态
   imgArr.value.forEach((item: any) => {
      table.value.toggleRowSelection(item, false)
   });
   // 复选框选中
   table.value.toggleRowSelection(row, true)
   // 收集图片的地址
   skuParams.skuDefaultImg = row.imgUrl
}
// 保存按钮的回调
const save = async () => {
   // 整理参数
   // 平台属性
   skuParams.skuAttrValueList = attrArr.value.reduce((prev: any, next: any) => {
      if (next.attrIdAndValueId) {
         let [attrId, valueId] = next.attrIdAndValueId.split(':')
         prev.push({
            attrId,
            valueId
         })
      }
      return prev
   }, [])
   // 销售属性
   skuParams.skuSaleAttrValueList = saleArr.value.reduce((prev: any, next: any) => {
      if (next.saleIdAndValueId) {
         let [saleAttrId, saleAttrValueId] = next.saleIdAndValueId.split(':')
         prev.push({
            saleAttrId,
            saleAttrValueId
         })
      }
      return prev
   }, [])
   // 发请求,添加sku
   let result = await reqAddsku(skuParams)
   if (result.code == 200) {
      ElMessage.success('添加SKU成功！')
      $emit('changeScene', { flag: 0, params: 'update' })
   } else {
      ElMessage.error('添加SKU失败！')
   }

}
// 获取添加SKU所需的相关数据
const initSkuData = async (c1Id: number | string, c2Id: number | string, spu: any) => {
   // 收集数据
   skuParams.category3Id = spu.category3Id
   skuParams.spuId = spu.id
   skuParams.tmId = spu.tmId
   // 获取平台属性
   let result: AttrResponseData = await reqAttr(c1Id, c2Id, spu.category3Id)
   // 获取对应的销售属性
   let result1: SpuSaleAttrResponseData = await reqSpuHasSaleAttr(spu.id)
   // 获取对应的照片墙数据
   let result2: SpuHasImgResponseData = await reqSpuImageList(spu.id)
   // 将数据存储到对应变量中
   attrArr.value = result.data
   saleArr.value = result1.data
   imgArr.value = result2.data

}
// 子组件方法暴露给父组件
defineExpose({ initSkuData })

</script>

<style lang="scss" scoped></style>